package com.yzsj.socket.init;

import com.yzsj.socket.business.dao.TagValueDao;
import com.yzsj.socket.business.domain.po.TagValuePO;
import com.yzsj.socket.business.domain.vo.TagValueVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wjl
 * @date 2018年11月5日15:43:24
 */
@Service
public class StartPubSub implements ApplicationListener<ContextRefreshedEvent> {

	private static Logger log = LoggerFactory.getLogger(StartPubSub.class);
	
	public static boolean refreshing = false;
	
	@Autowired
	private TagValueDao taginfoDao;

	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		if (event.getApplicationContext().getParent() == null) {
			init();
			log.info("开始整理变量");
		}
	}

	public void init() {
		List<TagValueVO> list = (List<TagValueVO>) taginfoDao.list(null);
		for(TagValueVO d : list){
			if(d.getTagid()!=null && d.getTagid()!=""){
				CSSTask.mapObj.put(d.getTagid(), d);
			}
		}

	}
	
	public void initRedis() {
		if (refreshing) {
			return;
		}
		refreshing = true;
		log.warn("正在初始化内存数据");
		clearRedisConf();
		init();
		log.warn("内存数据初始化完成");
		refreshing = false;
	}

	private void clearRedisConf() {
		CSSTask.mapObj.clear();
	}
}
